package com.itheima.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
    private static final AntPathMatcher PATH_MATCHER=new AntPathMatcher();
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        String[] urls=new String[]{
                "/employee/login",
                "/employee/logout",
                "/templates/**",
                "/front/**",
                "/common/**",
                "/user/sendMsg",
                "/user/login"
        };
        String requestURI = request.getRequestURI();
        boolean check = check(urls, requestURI);
        if (check){
            log.info("check为true通过:{}",requestURI);
            filterChain.doFilter(request,response);
            return;
        }else {
            if (request.getSession().getAttribute("employee")!=null){
                Long employee1 = (Long) request.getSession().getAttribute("employee");
                BaseContext.setCurrentId(employee1);
                log.info("存入ThreadLocal中id为"+employee1);
                log.info("存入ThreadLocal中id为然后取出id为"+BaseContext.getId());
                log.info("登陆过了，通过:{}",requestURI);
                filterChain.doFilter(request,response);
                return;
            } else if (request.getSession().getAttribute("user")!=null) {
                Long userId = (Long) request.getSession().getAttribute("user");
                BaseContext.setCurrentId(userId);
                filterChain.doFilter(request,response);
                return;
            } else {
                log.info("拦截到了{}",requestURI);
//                request.getRequestDispatcher("/templates/page/login/login.html").forward(request,response);
//                response.sendRedirect("http://localhost:8080/templates/page/login/login.html");
//                response.sendRedirect("https://www.baidu.com");

                response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
                return;
            }
        }


//
//        filterChain.doFilter(request,response);
    }
    public static boolean  check(String[] urls,String requestUri){
        for (String i:urls
             ) {
            boolean match = PATH_MATCHER.match(i, requestUri);
            if (match){
                return true;
            }
        }
        return false;
    }
}
